package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.zretc.service.PageDisplayService;
import com.zretc.service.impl.PageDisplayServiceImpl;

/**
 * Servlet implementation class PageDisplayServlet
 */
@WebServlet("/PageDisplayServlet")
public class PageDisplayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PageDisplayService pageDisplayServiceImpl = new PageDisplayServiceImpl();
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取op参数
		String op = request.getParameter("op");
		//判断op是否是请求渲染页面
		if ("pageDisplay".equals(op)) {
			//调用填充页面方法
			pageDisplay(request,response);
		}if ("fillRoomMessage".equals(op)) {
			//调用填充房间页面方法
			fillRoomMessage(request,response);
		} else if ("getAllHomestayPicture".equals(op)) {
			//调用获取指定民宿的所有照片
			getAllHomestayPicture(request,response);
		} else if ("findRoomByCondition".equals(op)) {
			//调用按筛选条件选择房间的方法
			findRoomByCondition(request,response);
		} else if ("findHoomByCondition".equals(op)) {
			//调用按筛选条件选择民宿的方法
			findHoomByCondition(request,response);
		} 
		
	}

	private void findHoomByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
				//获取省份
				String province = request.getParameter("province");
				//获取城市
				String city = request.getParameter("city");
				//获取区域
				String area = request.getParameter("area");
				//获取民宿风格
				String homeStyle = request.getParameter("homeStyle");
				HttpSession session = request.getSession();
		    	session.setAttribute("city",city);
				//构建一个map作为参数
				Map<String, String> paramsMap = new HashMap<String, String>();
				paramsMap.put("province", province);
				paramsMap.put("city", city);
				paramsMap.put("area", area);
				paramsMap.put("homeStyle", homeStyle);
				//调用服务层查询方法
				List<Map<String, String>> findRoomMessageList = pageDisplayServiceImpl.findHoomByCondition(paramsMap);
				System.out.println(findRoomMessageList);
				
				// 将findRoomMessageList包装成一个gson对象
				Gson gson = new Gson();
				String jsonList = gson.toJson(findRoomMessageList);
				// 将json对象响应给ajax请求
				response.getWriter().write(jsonList);
				
	}

	private void findRoomByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取风格
		String roomStyle = request.getParameter("roomStyle");
		//获取入住时间
		String inTime = request.getParameter("inTime");
		//获取离开时间
		String outTime = request.getParameter("outTime");
		//获取最高价
		String maxPrice = request.getParameter("maxPrice");
		//获取最低价
		String minPrice = request.getParameter("minPrice");
		//获取民宿编号
		String homestayId = request.getParameter("homestayId");
		
		//备注：如果没有选择条件的时候传来的是一个空串
		//构建一个map作为参数
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("roomStyle", roomStyle);
		paramsMap.put("inTime", inTime);
		paramsMap.put("outTime", outTime);
		paramsMap.put("maxPrice", maxPrice);
		paramsMap.put("minPrice", minPrice);
		paramsMap.put("homestayId", homestayId);
		//调用服务层查询方法
		List<Map<String, String>> findRoomMessageList = pageDisplayServiceImpl.findRoomByCondition(paramsMap);
		System.out.println(findRoomMessageList);
		// 将findRoomMessageList包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(findRoomMessageList);
		// 将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}

	private void getAllHomestayPicture(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//调用服务层根据指定民宿获取该民宿下的所有外观照片
		//获取民宿id
		String homestayId = request.getParameter("homestayId");
		//根据民宿编号获取该民宿的所有的外观照片
		List<String> homePictureList = pageDisplayServiceImpl.getHomestayPictureById(homestayId);
		// 将homePictureList包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(homePictureList);
		// 将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}

	private void fillRoomMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id
		String homestayId = request.getParameter("homestayId");
		String homestayName = request.getParameter("homestayName");
		System.out.println("123123"+homestayName);
		HttpSession session = request.getSession();
    	
		session.setAttribute("homestayName", homestayName);
		//根据民宿id查找该民宿下的所有房间
		List<Map<String, String>> roomMessageList = pageDisplayServiceImpl.getRoomMessageByHomestayId(homestayId);
		//将homestaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(roomMessageList);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}

	private void pageDisplay(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//调用业务层方法
		List<Map<String, String>> homestayMessageList =  pageDisplayServiceImpl.getAllHomestayMessage();
		//将homestaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(homestayMessageList);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	} 

}
